package com.jhhc.Weathers.datasupplement.controller;

import com.jhhc.Weathers.datasupplement.entity.BdmsWfProductF;
import com.jhhc.Weathers.datasupplement.entity.Params;
import com.jhhc.Weathers.datasupplement.entity.Supplement;
import com.jhhc.Weathers.datasupplement.entity.zksgBO.PathFile;
import com.jhhc.Weathers.datasupplement.service.JcdzService;
import com.jhhc.Weathers.datasupplement.util.FileUtil;
import com.jhhc.Weathers.datasupplement.util.NC_util;
import com.jhhc.utils.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

/**
 * @author LYQ
 */
@Api(value = "SupplementController",tags = {"数据补录服务"})
@RestController
@RequestMapping(value = "Weathers/datasupplement",headers = "Accept=application/json",method = RequestMethod.POST)
public class SupplementController {

    @Autowired
    private Params params;

    @Autowired
    private JcdzService jcdzService;

    /**
     * 中科曙光数据补录服务
     * @param supplement 参数实体类
     */
    @RequestMapping(value = "/zksg")
    public JsonResult zksg(@RequestBody Supplement supplement){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        if (supplement.getTimes() != null) {
            FileUtil fileUtil = new FileUtil();
            File file;
            for (String time : supplement.getTimes()) {
                System.out.println(time);
                String path = params.getPath() + File.separator + time + File.separator + "PRATEsfc.nc";
                file = new File(path);
                if (file.exists()) {
                    //雨量数据入库
                    List<BdmsWfProductF> fList = NC_util.readDate2(path, params.getProvideIdZ());
                    if (fList != null) {
                        int fs = jcdzService.selectSG(String.valueOf(fList.get(0).getProvideid()),
                                fList.get(0).getGridnm(), fList.get(0).getRtm());
                        if (fs > 0) {
                            System.out.println("数据已存在");
                        } else {
                            int num = jcdzService.insertBatch(fList);
                            System.out.println("预报时间：" + fList.get(0).getRtm() + "，雨量数据入库完成：" + num);
                        }
                    }
                }else {
                    System.out.println("文件不存在:" + path);
                }
            }
            jsonResult.setData("数据补录完成");
        }else {
            jsonResult.setData("参数为空，请检查");
        }
        return jsonResult;
    }
}
